#include "qdesigner.h"

#include "widgetboxtreewidget.h"
#include "qpropertylistview.h"
#include "qformlistwidget.h"
#include "qwidgetfactory.h"
#include "qaddformdialog.h"
#include "qremovepagedialog.h"
#include "qobjectlistview.h"
#include "qformwidget.h"

#include "../../lib/corelib/styledbar.h"
#include "../../lib/corelib/minisplitter.h"
#include "../../include/abstractcore.h"
#include "../../lib/shared/qproject.h"
#include "../../lib/corelib/qactiontoolbar.h"

#include <QVBoxLayout>

QDesigner::QDesigner(AbstractCore *core, QWidget *parent):
    QWidget(parent),
    m_core(core),
    m_splitter(new MiniSplitter(this)),
    m_widgetbox(new WidgetBoxTreeWidget(this)),
    m_widgetboxBar(new StyledBar),
    m_fileListCombo(new QComboBox),
    m_formlistToolBar(new QActionToolBar(this)),
    m_formlistBar(new StyledBar),
    m_listview(new QPropertyListView(m_core,this)),
    m_listviewBar(new StyledBar),
    m_objectListView(new QObjectListView(this))
{
    QWidgetFactory::m_core=m_core;

    m_formlist=new QFormListWidget(m_core,m_listview,this);
    connect(m_formlist,SIGNAL(updateFormName(int)),this,SLOT(updateFormName(int)));

    QVBoxLayout *l=new QVBoxLayout;
    l->setMargin(0);
    l->setSpacing(0);
    this->setLayout(l);

    l->addWidget(m_splitter);


    QWidget *wid;

    wid=new QWidget;
    l=new QVBoxLayout;
    l->setMargin(0);
    l->setSpacing(0);
    wid->setLayout(l);
    l->addWidget(m_widgetboxBar);
    l->addWidget(m_widgetbox);

    l=new QVBoxLayout;
    l->setContentsMargins(2,0,2,0);
    l->setSpacing(0);
    m_widgetboxBar->setLayout(l);
    m_fileListCombo->setProperty("panelwidget",true);
    QFont f=m_fileListCombo->font();
    f.setPointSize(12);
    m_fileListCombo->setFont(f);
    l->addWidget(m_fileListCombo);

    wid->resize(200,200);
    m_splitter->addWidget(wid);

    wid=new QWidget;
    l=new QVBoxLayout;
    l->setMargin(0);
    l->setSpacing(0);
    wid->setLayout(l);
    l->addWidget(m_formlistBar);
    l->addWidget(m_formlist);

    l=new QVBoxLayout;
    l->setMargin(2);
    l->setSpacing(0);
    m_formlistBar->setLayout(l);
    l->addWidget(m_formlistToolBar);

    QAction *ac;
    QList<QAction*> actionList;

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/append.png"));
    ac->setText(tr("Add New Form"));
    connect(ac,SIGNAL(triggered()),this,SLOT(add()));
    actionList.append(ac);

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/remove.png"));
    ac->setText(tr("Remove Form"));
    connect(ac,SIGNAL(triggered()),this,SLOT(remove()));
    actionList.append(ac);

    ac=new QAction(this);
    ac->setSeparator(true);
    actionList.append(ac);

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/left.png"));
    ac->setText(tr("Align left"));
    connect(ac,SIGNAL(triggered()),m_formlist,SLOT(alignLeft()));
    actionList.append(ac);

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/v-center.png"));
    ac->setText(tr("Vertical alignment"));
    connect(ac,SIGNAL(triggered()),m_formlist,SLOT(alignVCenter()));
    actionList.append(ac);

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/right.png"));
    ac->setText(tr("Align right"));
    connect(ac,SIGNAL(triggered()),m_formlist,SLOT(alignRight()));
    actionList.append(ac);

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/top.png"));
    ac->setText(tr("Align top"));
    connect(ac,SIGNAL(triggered()),m_formlist,SLOT(alignTop()));
    actionList.append(ac);

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/h-center.png"));
    ac->setText(tr("Horizontal alignment"));
    connect(ac,SIGNAL(triggered()),m_formlist,SLOT(alignHCenter()));
    actionList.append(ac);

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/bottom.png"));
    ac->setText(tr("Align bottom"));
    connect(ac,SIGNAL(triggered()),m_formlist,SLOT(alignBottom()));
    actionList.append(ac);

    ac=new QAction(this);
    ac->setSeparator(true);
    actionList.append(ac);

    m_formlistToolBar->addButtonActions(actionList);



    m_splitter->addWidget(wid);

    MiniSplitter *sp=new MiniSplitter(Qt::Vertical);

    wid=new QWidget;
    l=new QVBoxLayout;
    l->setMargin(0);
    l->setSpacing(0);
    wid->setLayout(l);
    l->addWidget(m_listviewBar);
    l->addWidget(m_listview);
    sp->addWidget(wid);

    m_objectListView->resize(200,200);

    sp->addWidget(m_objectListView);
    sp->setStretchFactor(0,1);
    sp->setStretchFactor(1,0);
    sp->resize(200,200);

    m_splitter->addWidget(sp);

    m_splitter->setStretchFactor(0,0);
    m_splitter->setStretchFactor(1,1);
    m_splitter->setStretchFactor(2,0);

    connect(QProject::project(),SIGNAL(openProject()),this,SLOT(projectOpen()));
    connect(QProject::project(),SIGNAL(beforeClosed()),this,SLOT(projectClosed()));
    connect(QProject::project(),SIGNAL(saveProject()),this,SLOT(projectSave()));
    connect(QProject::project(),SIGNAL(changed(int,int)),this,SLOT(projectChanged(int,int)));
}

void QDesigner::projectOpen()
{
    m_fileListCombo->clear();

    QProject *p=QProject::project();
    int     count=p->getPageCount();
    for(int i=0;i<count;i++)
    {
        m_fileListCombo->addItem(p->getPage(i).m_name);
    }
    m_objectListView->setFormWidget(m_formlist->currentForm());
    connect(m_fileListCombo,SIGNAL(currentIndexChanged(int)),this,SLOT(selectChanged(int)));
}

void QDesigner::projectClosed()
{
    disconnect(m_fileListCombo,SIGNAL(currentIndexChanged(int)),this,SLOT(selectChanged(int)));
    m_fileListCombo->clear();
}

void QDesigner::updateFormName(int index)
{
    QProject *p=QProject::project();
    if(index<0 || index>=p->getPageCount())
    {
        return;
    }
    QString str=p->getPage(index).m_name+(m_formlist->dirty(index)?" *":"");
    m_fileListCombo->setItemText(index,str);
}

void QDesigner::projectSave()
{
    tagPageInfo info;
    for(int i=0;i<m_fileListCombo->count();i++)
    {
        info=QProject::project()->getPage(i);
        m_fileListCombo->setItemText(i,info.m_name);
    }
}

void QDesigner::add()
{
    QAddFormDialog dlg(this);

    QStringList list;
    QProject* p=QProject::project();

    for(int i=0;i<p->getPageCount();i++)
    {
        list.append(p->getPage(i).m_name);
    }

    dlg.setPageNames(list);

    if(dlg.exec()==1)
    {
        QString name=dlg.getFormName();
        tagPageInfo info;
        info.m_name=name;
        info.m_type=dlg.getFormType();
        QProject::project()->insertPage(-1,info);
        m_formlist->showForm(name);
        m_fileListCombo->setCurrentIndex(m_fileListCombo->count()-1);
    }
}

void QDesigner::remove()
{
    QRemovePageDialog dlg(this);

    QProject *p=QProject::project();

    QStringList names;

    for(int i=0;i<p->getPageCount();i++)
    {
        names.append(p->getPage(i).m_name);
    }
    dlg.setPageNames(names);
    dlg.exec();

    names=dlg.getRemoveNames();

    foreach(QString name,names)
    {
        QProject::project()->removePage(name);
    }
}

void QDesigner::projectChanged(int type, int index)
{
    QProject *p=QProject::project();
    if(type==PCT_PAGE_DATA_ADD)
    {
        m_fileListCombo->insertItem(index,p->getPage(index).m_name);
    }
    else if(type==PCT_PAGE_DATA_DEL)
    {
        m_fileListCombo->removeItem(index);
    }
}

void QDesigner::selectChanged(int index)
{
    if(m_fileListCombo->currentIndex()>=0)
    {
        QString text=m_fileListCombo->currentText();
        if(text.endsWith(" *"))
        {
            text=text.left(text.size()-2);
        }
        m_formlist->showForm(text);
        m_objectListView->setFormWidget(m_formlist->currentForm());
        m_listview->setObject(m_formlist->currentForm());
    }
}
